package com.netease.nie.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.netease.nie.base.controllers.BaseController;
import com.netease.nie.base.exceptions.BusinessException;
import com.netease.nie.constants.BusinessConstants;
import com.netease.nie.dto.ConfirmedOrdersAddressDto;
import com.netease.nie.dto.MyDrawLotteryGoodsPrizeDto;
import com.netease.nie.dto.MyLuckyPrizeDto;
import com.netease.nie.dto.MyRobGoodsPrizeDto;
import com.netease.nie.dto.MyRobGoodsPrizeTipsDto;
import com.netease.nie.facade.ConfirmedOrdersAddressFacade;
import com.netease.nie.facade.MyLuckyPrizeFacade;

@RestController
@RequestMapping("/api/user")
public class MyLuckyPrizeController extends BaseController {

	@Autowired
	private MyLuckyPrizeFacade myLuckyPrizeFacade;
	
	@Autowired
	private ConfirmedOrdersAddressFacade confirmedOrdersAddressFacade;

	@RequestMapping(value = "/rob_luck/detail", method = RequestMethod.GET, produces = "application/json")
	public String robLuckyDetail(
			HttpServletRequest request,
			@RequestParam(name = "order_id", required = false) Long orderId,
			@RequestParam(name = "rob_goods_id", required = false) Integer robGoodsId,
			@RequestParam(name = "display_period", required = false) Integer displayPeriod)
			throws Exception {
		Long userId = (Long) request.getAttribute("userId");
		if (userId == null)
			throw new BusinessException(
					BusinessConstants.USER_NOT_LOGIN.getCode(), "用户未登录");
		if (userId <= 0)
			throw new BusinessException(
					BusinessConstants.INVALID_USER_ID.getCode(), "用户id不合法");

		MyRobGoodsPrizeDto myRobGoodsPrizeDto = myLuckyPrizeFacade
				.queryRobGoodsPrizeDtoByCondition(userId, orderId, robGoodsId,
						displayPeriod);
		return jsonSuccess(myRobGoodsPrizeDto);
	}

	@RequestMapping(value = "/draw_lottery_luck/detail", method = RequestMethod.GET, produces = "application/json")
	public String drawLotteryLuckyDetail(
			HttpServletRequest request,
			@RequestParam(name = "order_id", required = false) Long orderId,
			@RequestParam(name = "draw_lottery_goods_id") Integer drawLotteryGoodsId)
			throws Exception {
		Long userId = (Long) request.getAttribute("userId");
		
		if (userId == null)
			throw new BusinessException(
					BusinessConstants.USER_NOT_LOGIN.getCode(), "用户未登录");
		if (userId <= 0)
			throw new BusinessException(
					BusinessConstants.INVALID_USER_ID.getCode(), "用户id不合法");
		MyDrawLotteryGoodsPrizeDto myDrawLotteryGoodsPrizeDto = myLuckyPrizeFacade
				.queryMyDrawLotteryGoodsPrizeDtoByCondition(userId, orderId,
						drawLotteryGoodsId);
		return jsonSuccess(myDrawLotteryGoodsPrizeDto);
	}

	@RequestMapping(value = "/luck/list", method = RequestMethod.GET, produces = "application/json")
	public String luckyList(HttpServletRequest request, Integer page, Integer type) {
		Long userId = (Long) request.getAttribute("userId");
		
		if (userId == null)
			throw new BusinessException(
					BusinessConstants.USER_NOT_LOGIN.getCode(), "用户未登录");
		if (userId <= 0)
			throw new BusinessException(
					BusinessConstants.INVALID_USER_ID.getCode(), "用户id不合法");

		if (type == null)
			type = 1;

		if (page == null)
			page = 1;
		
		List<MyLuckyPrizeDto> myLuckyPrizeDtos = myLuckyPrizeFacade
				.queryMyLuckyPrizeDtosByCondition(userId, page, type);
		return jsonSuccess(myLuckyPrizeDtos);
	}
	
	@RequestMapping(value = "/confirmed_address", method = RequestMethod.POST, produces = "application/json")
	public String confirmedAddress(HttpServletRequest request,
			@RequestBody ConfirmedOrdersAddressDto confirmedOrdersAddressDto) {
		Long userId = (Long) request.getAttribute("userId");

		if (userId == null)
			throw new BusinessException(
					BusinessConstants.USER_NOT_LOGIN.getCode(), "用户未登录");
		if (userId <= 0)
			throw new BusinessException(
					BusinessConstants.INVALID_USER_ID.getCode(), "用户id不合法");
		int result = confirmedOrdersAddressFacade.confirmed(userId, confirmedOrdersAddressDto);
		return jsonSuccess(result);
	}
	
	@RequestMapping(value = "/rob_luck/tips", method = RequestMethod.GET, produces = "application/json")
	public String robLuckyTips(HttpServletRequest request) throws Exception {
		Long userId = (Long) request.getAttribute("userId");
		if (userId == null)
			throw new BusinessException(
					BusinessConstants.USER_NOT_LOGIN.getCode(), "用户未登录");
		if (userId <= 0)
			throw new BusinessException(
					BusinessConstants.INVALID_USER_ID.getCode(), "用户id不合法");
		MyRobGoodsPrizeTipsDto myRobGoodsPrizeTipsDto = myLuckyPrizeFacade.queryMyRobGoodsPrizeTipsDtoByUserId(userId);
		return jsonSuccess(myRobGoodsPrizeTipsDto);
	}
}
